<template>
<div class="processpanel">
      <el-dialog :close-on-click-modal="false" title="新增员工" :visible="dialogShow" @close="cancle">

         <el-form ref="myform" :model="form" label-width="100px" :rules="rules">

        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"  placeholder="1-30个字符"></el-input>
        </el-form-item>

         <el-form-item label="手机"  prop="mobile" >
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>

           <el-form-item label="入职时间"  prop="timeOfEntry">
             {{form.timeOfEntry}}
           <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="选择日期">
         </el-date-picker>
        </el-form-item>

        
           <el-form-item label="聘用形势"  prop="formOfEmployment">
         <el-select v-model="form.formOfEmployment">
           <el-option 
           v-for="(item,index) in hireType" 
           :key="index" 
           :label="item.value"
            :value="item.id">
            </el-option>
         </el-select>
        </el-form-item>

          <el-form-item label="工号"  prop="workNumber">
          <el-input v-model="form.workNumber"  placeholder="1-20字符"></el-input>
        </el-form-item>

   <el-form-item label="部门"   prop="departmentName">
     <div class="box"> 
       <el-input @focus="myfocus" v-model="form.departmentName" placeholder="请选择"></el-input>
        <el-tree
        :props="{label:'name'}"
        @node-click="nodeclick"
        v-if="showTree"
        :data="list"
         class="mytree"
          default-expand-all
           expand-on-click-node="false">
           </el-tree>
       </div>
        </el-form-item>

   <el-form-item label="转正日期"  prop="correctionTime">
           <el-date-picker v-model="form.correctionTime" type="date" placeholder="选择日期">
         </el-date-picker>        
         </el-form-item>
         </el-form>
         <div slot="footer" class="dialog-footer">
    <el-button @click="cancle">取 消</el-button>
    <el-button @click="submit" type="primary" >提 交</el-button>
  </div>
    </el-dialog>
</div>
    
</template>

<script>
//导入接口
import { getDepartmentApi } from "@/api/departments";
import { addEmployeesApi } from "@/api/employees";
// 导入枚举数据
import employeesEnus from "@/api/constant/employees";
import { async } from "q";
export default {
  // dialogShow: 控制面板的显示与隐藏
  props: ["dialogShow"],
  data() {
    return {
      //数据源
      form: {
        username: "", //姓名
        mobile: "", //手机
        timeOfEntry: "", //入职时间
        formOfEmployment: "", //聘用形势
        workNumber: "", //工号
        departmentName: "", //部门
        correctionTime: "" //转正日期
      },
      rules: {
        username: [
          {
            required: true,
            message: "必填",
            trigger: "blur"
          }
        ],
        mobile: [
          {
            required: true,
            message: "必填",
            trigger: "blur"
          },
          {
            min: 11,
            max: 11,
            message: "长度不正确",
            trigger: "blur"
          },
          {
            validator: (rules, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              reg.test(value)
                ? callback()
                : callback(new Error("手机号不正确"));
            }
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: "必填",
            trigger: "blur"
          }
        ],
        formOfEmployment: [
          {
            required: true,
            message: "必填",
            trigger: "blur"
          }
        ],
        workNumber: [
          {
            required: true,
            message: "必填",
            trigger: "blur"
          }
        ],
        departmentName: [
          {
            required: true,
            message: "必填",
            trigger: "change"
          }
        ],
        correctionTime: [
          {
            required: true,
            message: "必填",
            trigger: "blur"
          }
        ]
      },
      //选择器的数据源
      hireType: employeesEnus.hireType,
      //部门名称列表
      list: [],
      //控制树状结构是否显示
      showTree: false
    };
  },
  methods: {
    //提交数据
    submit() {
      //全局验证
      this.$refs.myform.validate(async isOk => {
        if (isOk) {
          //提交数据
          await addEmployeesApi(this.form);
          //其他处理
          this.$emit("update:dialogShow", false);
          this.form = {
            username: "", //姓名
            mobile: "", //手机
            timeOfEntry: "", //入职时间
            formOfEmployment: "", //聘用形势
            workNumber: "", //工号
            departmentName: "", //部门
            correctionTime: "" //转正日期
          };
          //清除表单校验
          this.$refs.myform.resetFields();
          //新增成功
          this.$message.success("新增成功");
          //调用父组件中的方法
          this.$emit("updateData");
        }
      });
    },
    //得到部门数据
    async getDepartment() {
      const res = await getDepartmentApi();
      // console.log(res);
      this.list = this.trunData(res.data.depts, "");
    },
    //转换树状结构
    trunData(arr, pid) {
      const newArr = [];
      arr.forEach(item => {
        if (item.pid === pid) {
          const children = this.trunData(arr, item.id);
          if (children.length > 0) {
            item.children = children;
          }
          newArr.push(item);
        }
      });
      return newArr;
    },
    //点击树状结构时会触发
    nodeclick(data) {
      // console.log(data);
      this.showTree = false;
      this.form.departmentName = data.name;
    },
    //部门输入框获取焦点时会触发
    myfocus() {
      this.showTree = true;
    },
    //关闭面板
    cancle() {
      this.$emit("update:dialogShow", false);
    }
  },
  created() {
    //得到部门数据
    this.getDepartment();
  }
};
</script>

<style lang="scss">
.processpanel {
  .box {
    position: relative;
    width: 300px;
    .mytree {
      position: absolute;
      border: 1px solid #ddd;
      border-radius: 5px;
      width: 100%;
      top: 44px;
      left: 0;
      z-index: 999;
      .el-tree-node__content {
        height: 40px;
        .el-icon-caret-right {
          display: none;
        }
        .el-tree-node__label {
          margin-left: 6px;
        }
      }
    }
  }
}
</style>
